import { LiftStatusChip, NameGradeCell } from '@/components'

import { GridColDef, GridValueGetterParams, GridRenderCellParams } from '@mui/x-data-grid-pro'

const useColumns = () => {
  const columns: GridColDef[] = [
    {
      field: 'name',
      headerName: '职级 + 用户名',
      width: 180,
      renderCell(params) {
        const { row } = params
        return <NameGradeCell row={row} copyKey="advisor_id" />
      }
    },
    {
      field: 'rank',
      headerName: '晋级阶段',
      align: 'center',
      renderCell(params: GridRenderCellParams) {
        const { row } = params
        return <LiftStatusChip row={row} />
      }
    },
    {
      field: 'credits',
      headerName: 'Credits',
      width: 120,
      type: 'number'
    },
    ...[
      { field: 'top_avg', label: 'top100付费时长' },
      { field: 'repurchase', label: '复购' },
      { field: 'new_user_conversion', label: '新用户转化' },
      { field: 'valid_easy_msg', label: '私信有效性' },
      { field: 'attendance', label: '出勤' },
      { field: 'order_complete', label: '完成率' },
      { field: 'comment_avg', label: '订单评分' }
    ].map((v) => ({
      field: v.field,
      headerName: v.label,
      minWidth: 120,
      valueGetter: (params: GridValueGetterParams) => {
        const { row } = params
        return get(row?.performance, v.field)
      }
    })),
    {
      field: 'admin_score',
      headerName: '管理员评分',
      width: 120,
      editable: true,
      valueParser(value) {
        // 验证输入是否为数字
        if (value && /^\d*$/.test(value)) {
          const intValue = parseInt(value, 10)
          const clampedValue = clamp(intValue, 0, 20)
          return clampedValue
        } else {
          return ''
        }
      }
    },
    {
      field: 'total_score',
      headerName: '绩效总分',
      width: 120
    }
  ]

  return columns
}

export default useColumns
